package top.kuanghua.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import top.kuanghua.goods.entity.TbCategory;
import top.kuanghua.goods.service.TbCategoryService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 商品类目表，类目和商品(spu)是一对多关系，类目与品牌是多对多关系(TbCategory)表控制层
 *
 * @author makejava
 * @since 2020-08-19 12:00:18
 */
@Api(tags = "商品类目表")
@RestController
@RequestMapping("tbCategory")
public class TbCategoryController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private TbCategoryService tbCategoryService;

    /**
     * 分页查询所有数据
     *
     * @param page       分页对象
     * @param tbCategory 查询实体
     * @return 所有数据
     */
    @GetMapping
    @ApiOperation(value = "分页查询所有数据")
    public R selectAll(Page<TbCategory> page, TbCategory tbCategory) {
        return success(this.tbCategoryService.page(page, new QueryWrapper<>(tbCategory)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "通过id主键查询单条数据")
    public R selectOne(@PathVariable Serializable id) {
        return success(this.tbCategoryService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param tbCategory 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation(value = "新增数据")
    public R insert(@RequestBody TbCategory tbCategory) {
        return success(this.tbCategoryService.save(tbCategory));
    }

    /**
     * 修改数据
     *
     * @param tbCategory 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation(value = "根据id修改数据")
    public R update(@RequestBody TbCategory tbCategory) {
        return success(this.tbCategoryService.updateById(tbCategory));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation(value = "根据id数组删除数据")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.tbCategoryService.removeByIds(idList));
    }
}